package br.albatroz.bean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.persistence.Query;

import br.albatroz.et.Time;

@ManagedBean(name="TimeBean")
public class TimeBean extends AbstractBean {

	private Time time;
	
	public TimeBean(){
		time = new Time();
	}
	
	public Time getTime() {
		return time;
	}

	public void setTime(Time time) {
		this.time = time;
	}

	public List<Time> getAllTime(){   
        return getEm().createQuery("select t from Time as t").getResultList(); 
	}
	
	public String saveTime(){
		try{
			getEm().getTransaction().begin();        
	        getEm().merge(time);       
	        getEm().getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			getEm().getTransaction().rollback();
		}
		
		time = new Time();
		
        return "rep-times";
	}
	
	public String editTime(){		
		try{			
			long id = Long.valueOf((String)getExternalParam().get("timeId"));
			Query q = getEm().createQuery("select j from Time as j where j.id = :id");
			q.setParameter("id", id);
			time = (Time) q.getSingleResult();
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		
		return "add-time";
		
	}
}
